import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.List;

import jxl.Workbook;
import jxl.read.biff.BiffException;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;


public class ExcelExport {

	
	


	static WritableFont arial10pt = new WritableFont(WritableFont.ARIAL, 10);
	static WritableCellFormat standardCellFormat =  new WritableCellFormat(arial10pt);
	static String inputFile = "Shit.xls";
	static final File OUTPUTTEMPLATE =  new File("out.xls");
	static int probandenWritten;
	static List<Proband[]> probandenPaare = new ArrayList<Proband[]>();
	
	
	
	private static void createSheet(WritableSheet sheet,ArrayList<Proband[]> probanden) throws WriteException, MalformedURLException{
		standardCellFormat.setWrap(true);
		System.out.println("Paaranzahl: "+ probanden.size());
		for (int i = 1; i < probanden.size(); i++) {
			createRow(sheet,probanden,i++);
			probandenWritten = probandenWritten+2;
		}
	}
	
	public static void main(String args[]) throws WriteException, IOException, BiffException{
		
		Workbook template = Workbook.getWorkbook(OUTPUTTEMPLATE);
		WritableWorkbook workbook =  Workbook.createWorkbook(new File("PrettyExcel.xls"), template);
		WritableSheet sheet = workbook.getSheet("shit");
		
		//grabs the input file and lets it be read by the ExceltoExcelMaping class
		ExcelImport inPutSheet = new ExcelImport();
		inPutSheet.importSheet(new File(inputFile));
		
		//Pairs up the stupid donors and Receivers
		Trading pairs = new Trading();
		List<List> donorsData = inPutSheet.getProbandenListen();
		pairs.trade(donorsData);
		
		
		//writes the shit that was read in
		ArrayList<Proband[]>probanden = (ArrayList<Proband[]>) pairs.getProbandOrdered();
		createSheet(sheet,probanden);
		workbook.write();
		workbook.close();
		System.out.println(probandenWritten+ " rules have been written in");
	}

	
	private static void addLabel(WritableSheet sheet, String column, int row, String s)
			throws WriteException, RowsExceededException {
		try{
		Label label;
		label = new Label(sheet.findCell(column).getColumn(), row, s,standardCellFormat);
		sheet.addCell(label);
		}
		catch (NullPointerException e) {
			// TODO: handle exception
		}
	}
	
	static void createRow(WritableSheet sheet, ArrayList<Proband[]> probanden, int row) throws RowsExceededException, WriteException{
		int i=row;
		
		addLabel(sheet,"Donor origin",i,probanden.get(i)[0].getOrigin());
		addLabel(sheet,"Donor id",i,probanden.get(i)[0].getId());
		addLabel(sheet,"Donor",i,probanden.get(i)[0].getName());
		addLabel(sheet,"Donor",i,probanden.get(i)[0].getLandValue(probanden.get(i)[1].getOrigin()));
		addLabel(sheet,"Receiver",i,probanden.get(i)[1].getName());
		addLabel(sheet,"Receiver id",i,probanden.get(i)[1].getId());
		addLabel(sheet,"Receiver origin",i,probanden.get(i)[1].getOrigin());
		
		row++;
		
		addLabel(sheet,"Donor origin",i,probanden.get(i)[1].getOrigin());
		addLabel(sheet,"Donor id",i,probanden.get(i)[1].getId());
		addLabel(sheet,"Donor",i,probanden.get(i)[1].getName());
		addLabel(sheet,"Donor",i,probanden.get(i)[1].getLandValue(probanden.get(i)[0].getOrigin()));
		addLabel(sheet,"Receiver",i,probanden.get(i)[0].getName());
		addLabel(sheet,"Receiver id",i,probanden.get(i)[0].getId());
		addLabel(sheet,"Receiver origin",i,probanden.get(i)[0].getOrigin());

		

	}
	
}
